<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <acc-set-card></acc-set-card>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div slot="header" class="clearfix">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="warning" @click="bkpfInfoReview">审核</el-button>
                <el-button type="warning" @click="bkpfInfoCancel">反审核</el-button>
                <el-button type="warning" @click="bkpfInfoPosting">过账</el-button>
                <el-button type="warning" @click="bkpfInfoUnposting">反过账</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>

                <!-- 搜索表单 -->
                <el-form :model="query" ref="searchRuleForm" label-width="80px">
                    <el-row style="margin-top:20px">
                        <el-col :sm="20" :md="5">
                            <el-form-item label="凭证号:" prop="vochNum">
                                <el-input
                                    placeholder="请输入内容"
                                    v-model="query.vochNum"
                                    size="small"
                                    class="handle-input mr10"
                                    disabled
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <!-- <el-form-item label="单据名称:" prop="orderNoId">
                                <div @click="showbillDialog('单别')">
                                    <el-input
                                        placeholder="请输入"
                                        v-model="query.orderNoId"
                                        size="small"
                                        readonly
                                    >
                                        <i slot="suffix" class="el-icon-search"></i>
                                    </el-input>
                                </div>
                            </el-form-item>-->
                            <el-form-item label="单别名称" prop="orderNoId">
                                <el-select
                                    v-model="query.orderNoId"
                                    placeholder="请选择"
                                    clearable
                                    @focus="getOrderNoList"
                                >
                                    <el-option
                                        :label="item.ruleName"
                                        :value="item.id"
                                        v-for="(item, index) in orderNoList"
                                        :key="index"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="审核码:" prop="conmStatus">
                                <el-select v-model="query.conmStatus" placeholder="请选择">
                                    <el-option label="Y" value="Y"></el-option>
                                    <el-option label="N" value="N"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="过账码:" prop="postStatus">
                                <el-select v-model="query.postStatus" placeholder="请选择">
                                    <el-option label="Y" value="Y"></el-option>
                                    <el-option label="N" value="N"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="凭证日期:" prop="vnDate">
                                <el-date-picker
                                    style="width:99%"
                                    v-model="query.vnDate"
                                    align="right"
                                    type="date"
                                    placeholder="选择日期"
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="会计期间" prop="period">
                                <el-select
                                    v-model="query.period"
                                    placeholder="请选择"
                                    clearable
                                    @focus="getPeriodList"
                                >
                                    <el-option
                                        :label="item"
                                        :value="item"
                                        v-for="(item, index) in periodList"
                                        :key="index"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5">
                            <el-form-item label="附件数:" prop="docNums">
                                <el-input
                                    type="number"
                                    min="1"
                                    placeholder="请输入内容"
                                    v-model="query.docNums"
                                    size="small"
                                    class="handle-input mr10"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :sm="20" :md="5" style="margin-left:10px">
                            <el-button
                                type="primary"
                                icon="el-icon-search"
                                @click="submitForm('searchRuleForm')"
                            >搜 索</el-button>
                            <el-button type="info" @click="resetForm('searchRuleForm')">重 置</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <vxe-table
                :data="tableData"
                border
                ref="xTree"
                :height="containerHeight - 300"
                @radio-change="currentChangeEvent"
            >
                <vxe-table-column type="radio" title width="100" tree-node></vxe-table-column>
                <vxe-table-column field="id" title="序号" type="seq" align="center"></vxe-table-column>
                <vxe-table-column field="vochNum" title="凭证号"></vxe-table-column>
                <vxe-table-column field="period" title="会计期间"></vxe-table-column>
                <vxe-table-column field="vnDate" title="凭证日期">
                    <template slot-scope="scope">{{scope.row.vnDate | formatDate}}</template>
                </vxe-table-column>
                <vxe-table-column field="docNums" title="附件数"></vxe-table-column>
                <vxe-table-column field="conmStatus" title="审核状态">
                    <template slot-scope="scope">{{scope.row.conmStatus == 'Y'?'审核通过':'未审核'}}</template>
                </vxe-table-column>
                <vxe-table-column field="postStatus" title="过账状态">
                    <template slot-scope="scope">{{scope.row.postStatus == 'Y'?'审核通过':'未审核'}}</template>
                </vxe-table-column>
                <vxe-table-column field="remark" title="备注"></vxe-table-column>
                <vxe-table-column title="明细">
                    <template slot-scope="scope">
                        <div @click="openDetail(scope.row.vochNum)">明细</div>
                    </template>
                </vxe-table-column>
            </vxe-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="pagination.pageIndex"
                    :page-size="pagination.pageSize"
                    :total="pagination.pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <bill-search
            :isShow="showBillSearch"
            :dialogTitle="billSearchDialogTit"
            method="orderNoRuleFindAllPage"
            @edit="billSearchBack"
            @close="closeDialog"
        />
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            billSearchDialogTit: '',
            showBillSearch: false,
            query: {
                conmStatus: '', // 审核码:N初始状态  Y审核状态，审核后才能过账 过账产生明细账、总账数据
                docNums: '', // 附件张数	默认0可以手工输入 format: int32
                orderNoId: '', // 单别id
                period: '', // 会计期间	模拟账套中当前会计期间，可以手工修改
                postStatus: '', // 过账码：N初始状态  Y审核通过状态
                vnDate: '', // 凭证日期	系统日期，可以调整	format: date-time
                vochNum: '' // 凭证单号:xxxxxxaaaa-200400001,6位公司id+4位单别-年月5位流水
            },
            loading: false,
            // 分页信息
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 60
            },
            showEditDialog: false,
            editForm: [],

            tableData: [],
            editData: {}, // 要修改的数据
            isEdit: false,
            multipleSelection: [],
            periodList: [],
            orderNoList: [], // 单别列表

            detailId: '' // 明细Id
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 200;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    methods: {
        // 选择单别
        showbillDialog(tit) {
            this.billSearchDialogTit = tit;
            this.showBillSearch = true;
        },
        // 单别搜索结果返回
        billSearchBack(data) {
            console.log(data.backData, 'billSearchBack');
            this.query.orderNoId = data.backData.id;
            this.showBillSearch = false;
        },
        closeDialog(data) {
            this.showBillSearch = false;
        },

        goEdit() {
            if (this.$refs.xTree.getRadioRecord() != null) {
                this.editForm = _.clone(this.$refs.xTree.getRadioRecord());
            } else {
                this.$message.info('请选择一条记录,再进行编辑');
                return;
            }

            console.log('goEdit', this.multipleSelection);
            this.$router.push({
                path: '/editGeneralLedger',
                query: {
                    id: this.editForm.vochNum
                }
            });
        },
        goAdd() {
            this.$router.push({
                path: '/editGeneralLedger'
            });
        },
        delAllSelection() {
            if (this.$refs.xTree.getRadioRecord() != null) {
                this.editForm = _.clone(this.$refs.xTree.getRadioRecord());
            } else {
                this.$message.info('请选择一条记录,再进行删除');
                return;
            }

            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteSubject();
                })
                .catch(() => {});
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //点击单选方法
        currentChangeEvent({ row }) {
            this.checkTableRow = row;
            console.log('currentChangeEvent', this.checkTableRow);
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.findAllPage();
        },
        deleteSubject() {
            appMain
                .bkpfInfoDel({}, this.editForm.vochNum)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.findAllPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },

        // 查询凭证列表
        findAllPage() {
            this.loading = true;
            let sendData = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query
            };
            appMain
                .bkpfInfoList(sendData)
                .then(res => {
                    this.timer = setTimeout(() => {
                        this.loading = false;
                    }, 1000);
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.tableData = res.data.data.list;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                    this.timer = setTimeout(() => {
                        //设置延迟执行
                        this.loading = false;
                    }, 1000);
                });
        },
        handlePageChange(val) {
            this.$set(this.pagination, 'pageIndex', val);
            console.log('pageChange:', this.pagination);
            //分页变化--请求接口
            this.findAllPage();
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.findAllPage();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        editClose() {
            this.showEditDialog = false;
        },
        // 打开明细弹窗
        openDetail(id) {
            this.$router.push({
                path: '/generalLedgerDetail',
                query: {
                    id: id
                }
            });
        },
        // 会计期间
        getPeriodList() {
            appMain
                .accPeriodFindDefaultData()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.periodList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 获取当前会计期间
        accPeriodDefaultData () {
            let d = new Date();
            let date_ = d.getFullYear() + '-' + appMain.formatZero(d.getMonth() + 1) + '-' + appMain.formatZero(d.getDate());
            appMain
                 .accPeriodDefaultData({}, date_ )
                .then(res => {
                    if (res.data.code === 20000) {
                        this.query.period = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 审核凭证
        bkpfInfoReview() {
            if (this.$refs.xTree.getRadioRecord() != null) {
                this.editForm = _.clone(this.$refs.xTree.getRadioRecord());
            } else {
                this.$message.info('请选择一条记录');
                return;
            }
            appMain
                .bkpfInfoReview({}, this.editForm.vochNum)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.findAllPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 反审核凭证
        bkpfInfoCancel() {
            if (this.$refs.xTree.getRadioRecord() != null) {
                this.editForm = _.clone(this.$refs.xTree.getRadioRecord());
            } else {
                this.$message.info('请选择一条记录');
                return;
            }
            appMain
                .bkpfInfoCancel({}, this.editForm.vochNum)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.findAllPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 过账
        bkpfInfoPosting() {
            if (this.$refs.xTree.getRadioRecord() != null) {
                this.editForm = _.clone(this.$refs.xTree.getRadioRecord());
            } else {
                this.$message.info('请选择一条记录');
                return;
            }
            debugger;
            appMain
                .bkpfInfoPosting({}, this.editForm.vochNum)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.findAllPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        bkpfInfoUnposting() {
            if (this.$refs.xTree.getRadioRecord() != null) {
                this.editForm = _.clone(this.$refs.xTree.getRadioRecord());
            } else {
                this.$message.info('请选择一条记录');
                return;
            }
            appMain
                .bkpfInfoUnposting({}, this.editForm.vochNum)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.findAllPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        // 单别列表
        getOrderNoList() {
            appMain
                .orderNoRuleFindAll()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.orderNoList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        }
    },
    created() {
        this.findAllPage();
        this.accPeriodDefaultData ()
    }
};
</script>

<style lang="css" scoped>
.el-card {
    height: 100%;
}
.flex-left ul {
    margin-top: 20px;
}

.flex-left li {
    box-shadow: 0px 0px 4px 2px #ddd;
    height: 30px;
    line-height: 30px;
    padding-left: 20px;
    margin-bottom: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
}
.flex-left li.current {
    box-shadow: 0px 0px 4px 2px #409eff;
}
.flex-right {
    padding: 20px;
}
</style>